package com.szgr.baoyin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.RedPacketListAdapter;
import com.szgr.baoyin.model.RedPacket;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class RedPacketActivity extends BaseActivity {

    private ListView redList;
    private RelativeLayout rlNoRed;
    private List<RedPacket> data;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    redList.setVisibility(View.VISIBLE);
                    rlNoRed.setVisibility(View.GONE);
                    listAdapter.notifyDataSetChanged();
                    break;
                case Constant.REQUEST_FAIL:
                    redList.setVisibility(View.GONE);
                    rlNoRed.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
    private RedPacketListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_red_packet);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("红包");
    }

    @Override
    public void initView() {
        data = new ArrayList<>();
        rlNoRed = (RelativeLayout) findViewById(R.id.no_record);
        redList = (ListView) findViewById(R.id.red_packet_list_view);
        listAdapter = new RedPacketListAdapter(this, data);
        redList.setAdapter(listAdapter);

        getRedPacket();
    }

    private void getRedPacket() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_RED_PACKET,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("--- red --->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONObject voucher_list = datas.getJSONObject("voucher_list");
                            Iterator<String> keys = voucher_list.keys();
                            Gson gson = new Gson();
                            while(keys.hasNext()){
                                JSONObject jsonObject1 = voucher_list.getJSONObject(keys.next());
                                RedPacket redPacket = gson.fromJson(jsonObject1.toString(), RedPacket.class);
                                data.add(redPacket);
                            }
                            mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                        } catch (JSONException e) {
                            mHandler.obtainMessage(Constant.REQUEST_FAIL).sendToTarget();
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }


}
